package com.tjvib.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.tjvib.Constants;
import com.tjvib.R;
import com.tjvib.base.BaseActivity;
import com.tjvib.presenter.BasePresenter;
import com.tjvib.util.LogUtil;
import com.tjvib.util.Manager.AppManager;
import com.tjvib.util.Manager.UserManager;
import com.tjvib.util.PoiUtil;
import com.tjvib.util.SharedPreferencesUtil;
import com.tjvib.util.ToastUtil;
import com.tjvib.util.retrofit.BaseInterceptor;
import com.tjvib.util.retrofit.BaseResponse;
import com.tjvib.util.retrofit.RetrofitHelper;
import com.tjvib.view.dialog.PrivacyDialog;
import com.tjvib.widget.AndroidScheduler;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class BootingActivity extends BaseActivity {

    private Button booting_btn_login, booting_btn_reg;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_booting;
    }

    @Override
    protected boolean initView() {
        booting_btn_login = findViewById(R.id.booting_btn_login);
        booting_btn_login.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View view) {
                Intent intent = new Intent(BootingActivity.this, LoginActivity.class);
                intent.putExtra("IS_LOGIN", true);
                startActivity(intent);
            }
        });

        booting_btn_reg = findViewById(R.id.booting_btn_reg);
        booting_btn_reg.setOnClickListener(new OnSingleClickListener() {
            @Override
            public void onSingleClick(View view) {
                Intent intent = new Intent(BootingActivity.this, LoginActivity.class);
                intent.putExtra("IS_LOGIN", false);
                startActivity(intent);
            }
        });
        return true;
    }

    @Override
    protected boolean initData() {
        if(SharedPreferencesUtil.getToken(this) != null){
            RetrofitHelper.getInstance().create()
                    .login_with_token()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidScheduler.mainThread())
                    .subscribe(new Observer<BaseResponse>() {
                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(BaseResponse value) {
                            LogUtil.i(value.getCode()+" "+value.getMessage()+" "+value.getData());
                            if(value.getCode() == Constants.SUCCESS){
                                String token = UserManager.getInstance().setInfo(String.valueOf(value.getData()));
                                if (!token.isEmpty())
                                    SharedPreferencesUtil.saveToken(BootingActivity.this, token);
                                else
                                    UserManager.getInstance().setToken(SharedPreferencesUtil.getToken(BootingActivity.this));

                                AppManager.getInstance().finishAllActivity();
                                startActivity(new Intent(BootingActivity.this, MainActivity.class));
                            }
                        }

                        @Override
                        public void onError(Throwable e) {
                            LogUtil.e(e.getMessage());
                            ToastUtil.show("token失效，请重新登陆");
                            PrivacyDialog privacyDialog = new PrivacyDialog(BootingActivity.this);
                            privacyDialog.showDialog(BootingActivity.this, view -> {
                                privacyDialog.dismiss();
                                AppManager.getInstance().exitApp();
                            });
                        }

                        @Override
                        public void onComplete() {

                        }
                    });

        }else{
            PrivacyDialog privacyDialog = new PrivacyDialog(this);
            privacyDialog.showDialog(this, view -> {
                privacyDialog.dismiss();
                AppManager.getInstance().exitApp();
            });
        }
        return true;
    }

    @Override
    protected BasePresenter genPresenter() {
        return null;
    }
}